import store from '@/store'

/**
 * @param {Array} value
 * @returns {Boolean}
 * @example see @/views/permission/directive.vue
 */
// export default function checkPermission(value) {
//   if (value && value instanceof Array && value.length > 0) {
//     const roles = store.getters && store.getters.roles
//     const permissionRoles = value

//     const hasPermission = roles.some(role => {
//       return permissionRoles.includes(role)
//     })
//     return hasPermission
//   } else {
//     console.error(`need roles! Like v-permission="['admin','editor']"`)
//     return false
//   }
// }

// 修改权限函数逻辑
// 根据函数传入的值，与菜单的编码比对，返回是否有权限
export default function checkPermission(value) {
  if (value && value.length > 0) {
    const permissions = store.getters.permissions
    const permissionCode = value

    const hasPermission = permissions.some(x => {
      return x.code === permissionCode && x.permissionType === 1
    })
    return hasPermission
  } else {
    console.error(`need permission! Like v-permission="system.user.add"`)
    return false
  }
}
